﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace SRF.Common.Validation
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
    public sealed class ClientCustomAttribute : ValidationAttribute, IClientValidatable
    {
        private const string _defaultErrorMessage = "校验不通过";
        private string funcName;

        public ClientCustomAttribute(string funcName)
            : base(_defaultErrorMessage)
        {
            this.funcName = funcName;
        }

        public override bool IsValid(object value)
        {
            return true;
        }
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule();
            rule.ErrorMessage = ErrorMessageString;
            rule.ValidationType = "custom";
            rule.ValidationParameters["func"] = this.funcName;

            return new[] { rule };
        }
    }
}